//
//  YGMainTabBar.swift
//  sinaWeiBoGui
//
//  Created by liyonggui on 16/5/10.
//  Copyright © 2016年 liyonggui. All rights reserved.
//

import UIKit

class YGMainTabBar: UITabBar {
    
    // MARK: - 属性
    var composeCallBack: (() -> ())?
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
//        print("\(composeButton)")
        
        // 计算宽度
        let width = frame.width * 0.2
        // 记录位置
        var index = 0
        
        for view in subviews {
//            print("\(view)")
            // UITabBarButton
            if view.isKindOfClass(NSClassFromString("UITabBarButton")!) {
                
                view.frame = CGRect(x: width * CGFloat(index), y: 0, width: width, height: self.frame.height)
//
                index++
                if index == 2 {
                    index++
                }
            }
            composeButton.frame = CGRect(x: width * 2, y: 0, width: width, height: self.frame.height)
            
        }
        
    }
    
    // MARK: - 按钮点击事件
    func composeButtonClick() {
        // 调用闭包
        composeCallBack?()
    }
    
    // MARK: - 懒加载
    lazy var composeButton: UIButton = {
       
        let button = UIButton()
        
        // tabbar_compose_button_highlighted  tabbar_compose_icon_add_highlighted
        ///  设置图片
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        
        /// 设置背景图片
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        // 添加绑定事件
        button.addTarget(self, action: "composeButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        // 添加到父容器
        self.addSubview(button)
        
        return button
    }()
    

}
